package com.lyf.scm.common.util.style;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;


/**
 * 单元格通用处理器（样式）
 *
 * @author wangchlei
 * @date 2020/08/18
 */
public class GeneralCellWriteHandler extends AbstractCellStyleStrategy implements BaseWriteHandler, RowWriteHandler {
    /**
     * 搜索标题样式
     */
    public CellStyle advancedSearchHeadCellStyle;
    /**
     * 日期标题样式
     */
    public CellStyle dateHeadCellStyle;
    /**
     * 内容通用样式（居右，宋体）
     */
    public CellStyle bodyGeneralCellStyleRight;
    /**
     * 内容通用样式（居左，微软雅黑）
     */
    public CellStyle bodyGeneralCellStyleLeft;

    public ExcelCellStyleHandler excelCellStyleHandler;

    @Override
    protected void initCellStyle(Workbook workbook) {
        this.excelCellStyleHandler = new ExcelCellStyleHandler(workbook);
        advancedSearchHeadCellStyle = excelCellStyleHandler.getAdvancedSearchHeadCellStyle();
        dateHeadCellStyle = excelCellStyleHandler.getDateHeadCellStyle();
        bodyGeneralCellStyleRight = excelCellStyleHandler.getBodyGeneralCellStyleRight();
        bodyGeneralCellStyleLeft = excelCellStyleHandler.getBodyGeneralCellStyleLeft();
//        cellStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));
    }


    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        //如果是非数据的标题
        if (MAIN_TITLES.contains(ExcelCellHandleUtil.getCellStringValue(cell))) {
            cell.setCellStyle(advancedSearchHeadCellStyle);
        } else {
            cell.setCellStyle(dateHeadCellStyle);
        }
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        //这里的标题每列只有一个标题，取第一个即可
        if (MAIN_TITLES.contains(head.getHeadNameList().get(0))) {
            cell.setCellStyle(bodyGeneralCellStyleLeft);
        } else {
            cell.setCellStyle(bodyGeneralCellStyleRight);
        }
//        if (cell != null && cell.getCellTypeEnum().compareTo(CellType.STRING)==0 && cell.getRichStringCellValue().getString().contains("%")) {
//            cell.setCellValue(Double.parseDouble(cell.getStringCellValue().replaceAll("%","")));
//            cell.setCellStyle(cellStyle);
//        }
    }

    @Override
    public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    /**
     * 设置标题行的行高（2倍于普同行）
     *
     * @param writeSheetHolder
     * @param writeTableHolder
     * @param row
     * @param relativeRowIndex
     * @param isHead
     * @return void
     **/
    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        if (isHead) {
            row.setHeight((short) (300 * 2));
        }
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {

    }
}
